package JDBC;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBC {
  public static void release(PreparedStatement ps,ResultSet rs, Connection connection) {
    if (ps != null) {
      try {
        ps.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if(connection != null )
    {
      try {
        connection.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if(rs != null)
    {
      try {
        connection.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

  }


  public static void release(PreparedStatement ps, Connection connection) {
    if (ps != null) {
      try {
        ps.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if(connection != null )
    {
      try {
        connection.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    }

  /**
   * 获取连接的操作
   *
   * @return
   * @throws IOException
   * @throws SQLException
   * @throws ClassNotFoundException
   */
  public static Connection getConnection() throws Exception {
    String driverClass = null;
    String jdbcUrl = null;
    String user = null;
    String password = null;


    InputStream in = JDBC.class.getClassLoader().getResourceAsStream("jdbc.properties");

    Properties properties = new Properties();

    properties.load(in);
    driverClass = properties.getProperty("driver");
    jdbcUrl = properties.getProperty("jdbcUrl");
    user = properties.getProperty("user");
    password = properties.getProperty("password");

    Class.forName(driverClass);
    Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
    return connection;
  }
}
